<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库单号" prop="receiptNo">
        <el-input
          v-model="queryParams.receiptNo"
          placeholder="请输入入库单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入库类型" prop="receiptType">
        <el-select v-model="queryParams.receiptType" placeholder="请选择入库类型" clearable>
          <el-option
            v-for="dict in dict.type.wms_receipt_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="receiptStatus">
        <el-select v-model="queryParams.receiptStatus" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.wms_receipt_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wms:receipt:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="receiptList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="入库单号" align="center" prop="receiptNo" />
      <el-table-column label="入库类型" align="center" prop="receiptType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_receipt_type" :value="scope.row.receiptType"/>
        </template>
      </el-table-column>
      <el-table-column label="仓库" align="center" prop="warehouseName" />
      <el-table-column label="供应商/客户" align="center" prop="supplierName" />
      <el-table-column label="计划入库时间" align="center" prop="plannedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.plannedTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="receiptStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wms_receipt_status" :value="scope.row.receiptStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['wms:receipt:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleAudit(scope.row)"
            v-hasPermi="['wms:receipt:audit']"
            v-if="scope.row.receiptStatus === '0'"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 入库单查看对话框 -->
    <el-dialog title="入库单详情" :visible.sync="viewOpen" width="780px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="入库单号">{{ viewForm.receiptNo }}</el-descriptions-item>
        <el-descriptions-item label="入库类型">
          <dict-tag :options="dict.type.wms_receipt_type" :value="viewForm.receiptType"/>
        </el-descriptions-item>
        <el-descriptions-item label="仓库">{{ viewForm.warehouseName }}</el-descriptions-item>
        <el-descriptions-item label="供应商/客户">{{ viewForm.supplierName || viewForm.customerName }}</el-descriptions-item>
        <el-descriptions-item label="来源单号">{{ viewForm.sourceOrderNo }}</el-descriptions-item>
        <el-descriptions-item label="状态">
          <dict-tag :options="dict.type.wms_receipt_status" :value="viewForm.receiptStatus"/>
        </el-descriptions-item>
        <el-descriptions-item label="计划入库时间">{{ parseTime(viewForm.plannedTime) }}</el-descriptions-item>
        <el-descriptions-item label="实际入库时间">{{ parseTime(viewForm.actualTime) }}</el-descriptions-item>
        <el-descriptions-item label="创建人">{{ viewForm.createBy }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ parseTime(viewForm.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ viewForm.remark }}</el-descriptions-item>
      </el-descriptions>
      <el-divider content-position="center">入库明细</el-divider>
      <el-table :data="viewForm.details">
        <el-table-column label="序号" type="index" width="55" align="center" />
        <el-table-column label="物料名称" prop="materialName" min-width="120" />
        <el-table-column label="物料编码" prop="materialCode" min-width="100" />
        <el-table-column label="计划数量" prop="plannedQuantity" width="100" />
        <el-table-column label="实际数量" prop="actualQuantity" width="100" />
        <el-table-column label="库区" prop="areaName" width="120" />
        <el-table-column label="库位" prop="locationName" width="120" />
        <el-table-column label="批次号" prop="batchNo" width="120" />
        <el-table-column label="生产日期" prop="productionDate" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.productionDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="过期日期" prop="expiryDate" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.expiryDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewOpen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 入库单审核对话框 -->
    <el-dialog title="入库单审核" :visible.sync="auditOpen" width="500px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="80px">
        <el-form-item label="审核结果" prop="auditStatus">
          <el-radio-group v-model="auditForm.auditStatus">
            <el-radio :label="'1'">通过</el-radio>
            <el-radio :label="'2'">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核意见" prop="auditComment">
          <el-input v-model="auditForm.auditComment" type="textarea" placeholder="请输入审核意见" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAudit">确 定</el-button>
        <el-button @click="auditOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核历史对话框 -->
    <el-dialog title="审核历史" :visible.sync="historyOpen" width="700px" append-to-body>
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in auditHistoryList"
          :key="index"
          :timestamp="parseTime(activity.auditTime)"
          :type="activity.auditStatus === '1' ? 'success' : 'danger'"
        >
          <el-card>
            <h4>{{ activity.auditBy }} - {{ activity.auditStatus === '1' ? '通过' : '驳回' }}</h4>
            <p>{{ activity.auditComment }}</p>
          </el-card>
        </el-timeline-item>
      </el-timeline>
      <div slot="footer" class="dialog-footer">
        <el-button @click="historyOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReceipt, getReceipt, auditReceipt } from "@/api/wms/receipt";
import { listAuditHistory } from "@/api/wms/receipt-audit";

export default {
  name: "ReceiptAudit",
  dicts: ['wms_receipt_type', 'wms_receipt_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库单表格数据
      receiptList: [],
      // 是否显示查看弹出层
      viewOpen: false,
      // 是否显示审核弹出层
      auditOpen: false,
      // 是否显示审核历史弹出层
      historyOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        receiptNo: null,
        receiptType: null,
        receiptStatus: null
      },
      // 日期范围
      dateRange: [],
      // 查看表单参数
      viewForm: {
        receiptId: null,
        receiptNo: null,
        receiptType: null,
        warehouseName: null,
        supplierName: null,
        customerName: null,
        sourceOrderNo: null,
        plannedTime: null,
        actualTime: null,
        receiptStatus: null,
        createBy: null,
        createTime: null,
        remark: null,
        details: []
      },
      // 审核表单参数
      auditForm: {
        receiptId: null,
        auditStatus: "1",
        auditComment: null
      },
      // 审核历史列表
      auditHistoryList: [],
      // 审核表单校验
      auditRules: {
        auditStatus: [
          { required: true, message: "审核结果不能为空", trigger: "change" }
        ],
        auditComment: [
          { required: true, message: "审核意见不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询入库单列表 */
    getList() {
      this.loading = true;
      // 默认只查询待审核的入库单
      if (this.queryParams.receiptStatus === null || this.queryParams.receiptStatus === '') {
        this.queryParams.receiptStatus = '0';
      }
      listReceipt(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.receiptList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.receiptStatus = '0';
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.receiptId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleView(row) {
      getReceipt(row.receiptId).then(response => {
        this.viewForm = response.data;
        this.viewOpen = true;
      });
    },
    /** 审核按钮操作 */
    handleAudit(row) {
      this.auditForm = {
        receiptId: row.receiptId,
        auditStatus: "1",
        auditComment: null
      };
      this.auditOpen = true;
    },
    /** 提交审核 */
    submitAudit() {
      this.$refs["auditForm"].validate(valid => {
        if (valid) {
          auditReceipt(this.auditForm).then(response => {
            this.$modal.msgSuccess("审核成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 查看审核历史 */
    handleAuditHistory(row) {
      listAuditHistory(row.receiptId).then(response => {
        this.auditHistoryList = response.data;
        this.historyOpen = true;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/receipt/export', {
        ...this.queryParams
      }, `receipt_audit_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>